package hivMultiCompartment;

import java.util.ArrayList;
import java.util.Iterator;

public class CD8TCell extends TCell {
	
	public boolean CD4Stimulation;
	public double killingEfficiency;
	
	public CD8TCell() {
		super ();		
		
		avgLife = GlobalConstants.avgLifeCD8TNaive;
		life = GlobalConstants.sampleNormal(avgLife);
		
		moveInterval = GlobalConstants.moveIntervalCD8T;
		
		probEnterLymph = GlobalConstants.probEnterLymphCD8Naive;
		probEnterThymus = probEnterLymph + GlobalConstants.probEnterThymusCD8Naive;
		probExit = GlobalConstants.probExitCD8TNaive;
		
		CD4Stimulation = false;
		
		limitedPorliferation = false;
		proliferateCount = GlobalConstants.dividingCountCD8TNaive;
		proliferateInterval = GlobalConstants.doublingTimeCD8TNaive;
		proliferateCD = proliferateInterval;
		
		setColor(GlobalConstants.colorCD8TNaive);
		killingEfficiency = 0;
		recognizeStrain = Model.getUniformIntFromTo(1, GlobalConstants.maxIdiotypeCD8T);
		
	}
	
	public void statesAndInteractions(){
		if(!alive) return;
		switch(state){
			case ACTIVE:
				//search for CD4 for additional stimulation
				if(Organ2D != null) {
					if(getCD4TEffector()) {
						double r = Model.getUniformDoubleFromTo(0, 1);
						if(r < GlobalConstants.probCD4EffectorActivateCD8Active){
							CD4Stimulation = true;
						}
					}
				}
				//final differentiation
				if(proliferateCount == 0) {
					if(CD4Stimulation) {
						double r = Model.getUniformDoubleFromTo(0, 1);
						if (r < GlobalConstants.memoryFrqCD8T) {
							//System.out.println("CD8 Memory");
							changeState(AgentState.MEMORY);
						}else {
							changeState(AgentState.EFFECTOR);
						}
					}else{
						changeState(AgentState.EFFECTOR);
						killingEfficiency = GlobalConstants.killingEfficiencyActive;
					}
				}
				break;
//			case EFFECTOR:
//				// killing conducted by infected cells;
//				break;
//			case MEMORY:
//				// reactivation conducted by infected cells
//				break;
			default:
				//naive
				break;
		}
	}
	
	private boolean getCD4TEffector(){
		boolean result = false;
		ArrayList<Object> cList = Organ2D.cellSpace.getMooreNeighborOfType(x, y, GlobalConstants.interationRadius, CD4TCell.class);
		Iterator<Object> cIter = cList.iterator();
		while(cIter.hasNext()) {
			CD4TCell c = (CD4TCell)cIter.next();
			if(c.isAlive() && c.isEffector()) {
				result = true;
				break;
			}
		}
		return result;
	}
	public void activate(){
		changeState(AgentState.ACTIVE);
	}
	
	public void changeState(AgentState s) {
		state = s;
		switch(s){
			case ACTIVE:
				avgLife = GlobalConstants.avgLifeCD8TActive;
				life = GlobalConstants.sampleNormal(avgLife);
				probExit = GlobalConstants.probExitCD8TActive;
				probEnterLymph = GlobalConstants.probEnterLymphCD8Active;
				probEnterThymus = GlobalConstants.probEnterThymusCD8Active;
				proliferateCount = GlobalConstants.dividingCountCD8TActive;
				proliferateInterval = GlobalConstants.doublingTimeCD8TActive;
				proliferateCD = proliferateInterval;
				limitedPorliferation = true;
				break;
			case EFFECTOR:
				avgLife = GlobalConstants.avgLifeCD8TEffector;
				life = GlobalConstants.sampleNormal(avgLife);
				probExit = GlobalConstants.probExitCD8TEffector;
				probEnterLymph = GlobalConstants.probEnterLymphCD8Effector;
				probEnterThymus = GlobalConstants.probEnterThymusCD8Effector;
				killingEfficiency = GlobalConstants.killingEfficiencyEffector;
				proliferateCount = 0;
				break;
			case MEMORY:
				avgLife = GlobalConstants.avgLifeCD8TMemory;
				life = GlobalConstants.sampleNormal(avgLife);
				probExit = GlobalConstants.probExitCD8TMemory;
				probEnterLymph = GlobalConstants.probEnterLymphCD8Memory;
				probEnterThymus = GlobalConstants.probEnterThymusCD8Memory;
				proliferateCount = 0;
				limitedPorliferation = true;
				break;
			default:
				//naive
				break;
		}
	}
}
